﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace EasySL.Controls
{
    public class Window
    {  
        private static ChildWindow confirmWindow;
        private static ChildWindow alertWindow;

        public static Thickness ContentMargin = new Thickness(5);
        public static ImageSource AlertImageSource = GetImage("alert");
        public static object AlertTitle = "Alert";
        public static object ConfirmTitle = "Confirm";

        static Window()
        {
            alertWindow = new ChildWindow();    
            confirmWindow = new ChildWindow();       
        }

        public static void Alert(object content)
        {
            var sp = new StackPanel();
            sp.Orientation = Orientation.Horizontal;

            var image = new Image();
            image.Margin = ContentMargin;
            image.Stretch = Stretch.None;
            image.Source = AlertImageSource;

            var cp = new ContentPresenter();
            cp.Margin = ContentMargin;

            sp.Children.Add(image);
            sp.Children.Add(cp);

            alertWindow.Title = AlertTitle;
            alertWindow.Content = sp;

            if (content is string)
            {
                TextBlock tb = new TextBlock();
                tb.Text = content.ToString();
                tb.MaxWidth = 400;
                tb.TextWrapping = TextWrapping.Wrap;
                cp.Content = tb;
            }
            else
            {
                cp.Content = content;
            }

            alertWindow.Show();
        }

        public static void Confirm(object content, Action yes)
        {
            var sp = new StackPanel();

            var cp = new ContentPresenter();
            cp.Margin = ContentMargin;
            cp.Content = content;

            var sp2 = new StackPanel();
            sp2.Orientation = Orientation.Horizontal;
            sp2.HorizontalAlignment = HorizontalAlignment.Center;

            var btYes = new HyperlinkButton() { Content = "Yes", Margin = ContentMargin };
            btYes.Click += (s, e) =>
            { 
                confirmWindow.Close();
                if (yes != null)
                    yes();
            };

            var btNo = new HyperlinkButton() { Content = "No", Margin = ContentMargin };
            btNo.Click += (s, e) => confirmWindow.Close();

            sp2.Children.Add(btYes);
            sp2.Children.Add(btNo);

            sp.Children.Add(cp);
            sp.Children.Add(sp2);

            confirmWindow.Title = ConfirmTitle;
            confirmWindow.Content = sp;

            confirmWindow.Show();
        }

        private static ImageSource GetImage(string imageName)
        {
            string imageUrl = string.Format("/EasySL.Controls;component/Images/{0}.png", imageName);
            return new BitmapImage(new Uri(imageUrl, UriKind.RelativeOrAbsolute));
        }

    }
}
